#
# Author: Kang Lin<kl222@126.com>
# Date  : 2019-08-20
#

cmake_minimum_required(VERSION 2.8)
project(SeetaNet)

# option for install
#set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install CACHE STRING "set install prefix" FORCE)

# set flags
if(UNIX)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wno-sign-compare -Wno-unused-parameter")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -fpermissive -std=c++11 -Wno-sign-compare -Wno-unused-parameter")
    if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
        set(CMAKE_C_FLAGS "-O0 -g -ggdb ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-O0 -g -ggdb ${CMAKE_CXX_FLAGS}")
    else()
        set(CMAKE_C_FLAGS "-O2 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-O2 ${CMAKE_CXX_FLAGS}")
    endif()
    if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
        set(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-m32 ${CMAKE_CXX_FLAGS}")
    elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x64")
        set(CMAKE_C_FLAGS "-m64 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-m64 ${CMAKE_CXX_FLAGS}")
    elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
        set(CMAKE_C_FLAGS "-m64 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-m64 ${CMAKE_CXX_FLAGS}")
    endif()
    if(SEETA_USE_SSE2)
        set(CMAKE_C_FLAGS "-msse2 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-msse2 ${CMAKE_CXX_FLAGS}")
    endif()
    if(SEETA_USE_FMA)
        set(CMAKE_C_FLAGS "-mfma ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-mfma ${CMAKE_CXX_FLAGS}")
    endif()
elseif(MSVC)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()

add_definitions(-DSEETA_EXPORTS)

# search files
FILE(GLOB_RECURSE INCLUDE_FILES
        ${PROJECT_SOURCE_DIR}/include/*.h
        ${PROJECT_SOURCE_DIR}/include/*.hpp
        )
FILE(GLOB_RECURSE SRC_FILES
        ${PROJECT_SOURCE_DIR}/src/*.c
        ${PROJECT_SOURCE_DIR}/src/*.cpp
        ${PROJECT_SOURCE_DIR}/src/*.cc
        )
FILE(GLOB_RECURSE SRC_INCLUDE_FILES
        ${PROJECT_SOURCE_DIR}/src/*.h
        ${PROJECT_SOURCE_DIR}/src/*.hpp
        )
    
# for compile
add_library(${PROJECT_NAME} ${SRC_FILES})
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/src/include_inner
    ${PROJECT_SOURCE_DIR}/src/include_inner/layers
    ${PROJECT_SOURCE_DIR}/src
    )
if(ANDROID_ARM_NEON)
    target_compile_definitions(${PROJECT_NAME} PUBLIC __ARM_NEON__)
endif()

GENERATE_EXPORT_HEADER(${PROJECT_NAME}
    EXPORT_MACRO_NAME SEETA_NET_API
    EXPORT_FILE_NAME  SeetaNetExport.h
    )
file(COPY ${CMAKE_CURRENT_BINARY_DIR}/SeetaNetExport.h
    DESTINATION ${CMAKE_BINARY_DIR})
install(
  FILES
    ${CMAKE_CURRENT_BINARY_DIR}/SeetaNetExport.h
  DESTINATION
    ${CMAKE_INSTALL_INCLUDEDIR}/seeta
)
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_BINARY_DIR}
    )

set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${BUILD_VERSION})
# Be will to install header files
set_property(
    TARGET
        ${PROJECT_NAME}
    PROPERTY
        PUBLIC_HEADER ${INCLUDE_FILES}
)

# link library
if (NOT ANDROID AND UNIX) 
    target_link_libraries(${PROJECT_NAME} pthread)   
endif()
if (WIN32) 
    target_link_libraries(${PROJECT_NAME} Ws2_32)   
endif()

export(TARGETS ${PROJECT_NAME}
       APPEND FILE ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
)

INSTALL(TARGETS ${PROJECT_NAME}
    EXPORT ${PROJECT_NAME}Config
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}"
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/seeta
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )
# Install cmake configure files
install(
        EXPORT ${PROJECT_NAME}Config
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake"
        NAMESPACE
            SeetaFace::
)
write_basic_package_version_file(
    "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
    VERSION ${BUILD_VERSION}
    COMPATIBILITY AnyNewerVersion)
install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")

